package cn.im.qywx.dao;

import cn.im.qywx.model.MeetingReserve;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public interface MeetingReserveDao extends JpaRepository<MeetingReserve,Long> {


    /**
     * 根据id、enable查找 MeetingReserve
     * @param id
     * @param enable
     * @return
     */
    public MeetingReserve findByIdAndEnable(Long id,Integer enable);



    @Query(nativeQuery = true,value = "select * from IM_QYWX_MEETING_RESERVE where enable = 1 and meeting_date >= date_format(:today,'%Y-%m-%d') order by meeting_date")
	public List<MeetingReserve> findByMeetingDate(@Param("today") String today);


    @Query(nativeQuery = true,value = "select count(*) from (select * from IM_QYWX_MEETING_RESERVE e where e.enable=1 and " +
            "ROOM_ID = :roomId and e.meeting_date = date_format(:timeDate,'%Y-%m-%d')) r where  (:startTime >= r.start_time and :startTime < r.end_time) or  " +
            "(:endTime > r.start_time and :endTime <= r.end_time) or (:startTime <= r.start_time and :endTime >= r.end_time)")
    public int countExist(@Param("roomId") Long roomId,@Param("timeDate") String timeDate,@Param("startTime") int startTime,@Param("endTime") int endTime);



    @Modifying
    @Query("update MeetingReserve m set m.enable = 0 where m.id = :id and m.wxno = :wxno")
    int updateMeetingReserve(@Param("id") Long id,@Param("wxno") String wxno);
}
